import { Controller } from "../../mvc/core/Controller";
import { ISystemSettingController } from "./interfaces/ISystemSettingController";
import { SystemSetter } from "../view/system/SystemSetter";
import { IView } from "../../mvc/interfaces/IView";
import { SystemSettingModule } from "../../../modules/SystemSettingModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SSysSetting } from "../../../Message/Game/SSysSetting";
import { SoundManager } from "../../common/sound/SoundManager";
import { SClientSetting } from "../../../Message/Game/SClientSetting";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { SPlayerLockStatus } from "../../../Message/Game/SPlayerLockStatus";
import { LockInfoData } from "../view/lock/LockInfoData";
import { SSysShortcut } from "../../../Message/Game/SSysShortcut";
import { ShortcutsKey } from "../../common/shortcutsKey/ShortcutsKey";
import { SystemSetterInfo } from "../view/system/SystemSetterInfo";
import { ISystemSettingModule } from "../../../modules/interfaces/ISystemSettingModule";
//class SystemSettingController
    
    export  class SystemSettingController extends Controller implements ISystemSettingController
    {
        constructor()
        {
            
            super();this._sysSetterInfo = SystemSetter.localSetter;
            this._timer = new Object /* flash.utils.Timer */(500, 2);
            return;
        }

        public get isGuideInfoGot(): Boolean
        {
            return this._isGuideInfoGot;
        }

        protected /* override */ initView(): IView
        {
            if (this._systemModule == null) 
            {
                this._systemModule = new SystemSettingModule();
            }
            return this._systemModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.SaveSystemSetter, this.onSaveSystemSetHandler);
            Dispatcher.addEventListener(EventName.SaveGuideSystemSetter, this.onSaveGuideSystemSetter);
            Dispatcher.addEventListener(EventName.SaveForSliderChange, this.onSaveSliderSetHandler);
            Dispatcher.addEventListener(EventName.BackGroundMusicOpenOrClose, this.onBackGroundMusicOpenOrCloseHandler);
            Dispatcher.addEventListener(EventName.Sys_SaveInfoNotShowInWindow, this.onSaveInfoNotShowInWindowHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateSysSetting, this.onReceiveSysSetterHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGatePlayerClientSetting, this.onGatePlayerClientSetting);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGatePlayerLockStatus, this.onPlayerLockStatusHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
            Dispatcher.addEventListener(EventName.HeaderSliderChange, this.onHeadChangeHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGatePlayerSysShortcut, this.onPlayerSysShortcutHandler);
            Dispatcher.addEventListener(EventName.SaveShortcutSetter, this.onSaveShortcutSetter);
            return;
        }

        /* internal  */onSaveSystemSetHandler(arg1: DataEvent): void
        {
            this._isSet = true;
            if (arg1.data == null) 
            {
                this._sysSetInfoStr = SystemSetter.localSetter.getString();
            }
            else 
            {
                this._sysSetInfoStr = arg1.data as string;
            }
            this.setSystemSetterInfo(this._sysSetInfoStr);
            return;
        }

        /* internal  */onSaveGuideSystemSetter(arg1: DataEvent): void
        {
            var loc1=SystemSetter.systemGuideSetterInfo.getString();
            GameProxy.sysSetterProxy.saveGuideSystemSetterInfo(loc1);
            return;
        }

        /* internal  */onSaveInfoNotShowInWindowHandler(arg1: DataEvent): void
        {
            SystemSetter.localSetter.isAlter = true;
            this._sysSetInfoStr = SystemSetter.localSetter.getString();
            this.setSystemSetterInfo(this._sysSetInfoStr, true, true);
            return;
        }

        /* internal  */onSaveSliderSetHandler(arg1: DataEvent): void
        {
            this.saveDataForSilderChange();
            return;
        }

        /* internal  */saveDataForSilderChange(): void
        {
            SystemSetter.localSetter.isAlter = true;
            this._sysSetInfoStr = SystemSetter.localSetter.getString();
            this.setSystemSetterInfo(this._sysSetInfoStr, false);
            if (!this._timer.running) 
            {
                this._timer.start();
                this._timer.addEventListener(flash.events.TimerEvent.TIMER_COMPLETE, this.onTimerCompleteHandler);
            }
            return;
        }

        /* internal  */onTimerCompleteHandler(arg1: Object /* flash.events.TimerEvent */): void
        {
            if (SystemSetter.localSetter.isAlter && this._isReviceSet) 
            {
                GameProxy.sysSetterProxy.saveSystemSetterInfo(this._sysSetInfoStr);
                SystemSetter.localSetter.isAlter = false;
            }
            this._timer.stop();
            this._timer.reset();
            this._timer.removeEventListener(flash.events.TimerEvent.TIMER_COMPLETE, this.onTimerCompleteHandler);
            return;
        }

        /* internal  */onReceiveSysSetterHandler(arg1: MessageBlock): void
        {
            var loc3=null;
            var loc1=arg1.messageBase as SSysSetting;
            var loc2=loc1.jsStr;
            this._isReviceSet = true;
            if (loc2 == "") 
            {
                if (!this._isSetDefault) 
                {
                    this._isSetDefault = true;
                    loc3 = SystemSetter.currentSetter.getString();
                    this.setSystemSetterInfo(loc3);
                }
            }
            else 
            {
                SystemSetter.currentSetter.deserializeInfo(loc2);
                SystemSetter.localSetter.copy(SystemSetter.currentSetter, true);
                this._sysSetInfoObj = SystemSetter.localSetter.getSetterObject();
                cache.autoResumeSet.roleLifeSet = SystemSetter.localSetter.roleLife;
                cache.autoResumeSet.roleManaSet = SystemSetter.localSetter.roleMana;
                cache.autoResumeSet.petLifeSet = SystemSetter.localSetter.petLife;
                cache.autoResumeSet.petManaSet = SystemSetter.localSetter.petMana;
                cache.autoResumeSet.isSaveSysset = true;
                SoundManager.instance.musicVolume = SystemSetter.currentSetter.musicVolume;
                SoundManager.instance.effectSoundVolune = SystemSetter.currentSetter.effectSoundVolume;
                Dispatcher.dispatchEvent(new DataEvent(EventName.SysSettingSuccess));
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.NoSoundBtnShowOrHide, SystemSetter.currentSetter.haveNoSound));
            Dispatcher.dispatchEvent(new DataEvent(EventName.SysSettingRes));
            return;
        }

        /* internal  */onGatePlayerClientSetting(arg1: MessageBlock): void
        {
            var loc4=null;
            var loc1=arg1.messageBase as SClientSetting;
            var loc2=loc1.jsStr;
            this._isGuideInfoGot = true;
            var loc3=SystemSetter.systemGuideSetterInfo.mapInfo;
            if (loc2 == "") 
            {
                if (!this._isClientDefault) 
                {
                    this._isClientDefault = true;
                    loc5 = 0;
                    loc6 = loc3;
                    for (loc4 in loc6) 
                    {
                        if (!cache.clientSettingCache.hasOwnProperty(loc4)) 
                        {
                            continue;
                        }
                        cache.clientSettingCache[loc4] = SystemSetter.systemGuideSetterInfo[loc4];
                    }
                }
            }
            else 
            {
                SystemSetter.systemGuideSetterInfo.deserializeInfo(loc2);
                var loc5=0;
                var loc6=loc3;
                for (loc4 in loc6) 
                {
                    if (!cache.clientSettingCache.hasOwnProperty(loc4)) 
                    {
                        continue;
                    }
                    cache.clientSettingCache[loc4] = SystemSetter.systemGuideSetterInfo[loc4];
                }
            }
            return;
        }

        /* internal  */onBackGroundMusicOpenOrCloseHandler(arg1: DataEvent): void
        {
            if (this._systemModule && !this._systemModule.isHide) 
            {
                (view as SystemSettingModule).haveNoSound = !(view as SystemSettingModule).haveNoSound;
            }
            else 
            {
                this._sysSetterInfo.haveNoSound = !this._sysSetterInfo.haveNoSound;
                if (this._sysSetterInfo.haveNoSound) 
                {
                    this._sysSetterInfo.musicVolume = 0;
                    this._sysSetterInfo.effectSoundVolume = 0;
                }
                else 
                {
                    this._sysSetterInfo.musicVolume = 0.3;
                    this._sysSetterInfo.effectSoundVolume = 0.3;
                }
                SoundManager.instance.musicVolume = this._sysSetterInfo.musicVolume;
                SoundManager.instance.effectSoundVolune = this._sysSetterInfo.effectSoundVolume;
                if (SystemSetter.currentSetter.haveNoSound != this._sysSetterInfo.haveNoSound) 
                {
                    SystemSetter.currentSetter.haveNoSound = this._sysSetterInfo.haveNoSound;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.NoSoundBtnShowOrHide, SystemSetter.currentSetter.haveNoSound));
                }
                this.saveDataForSilderChange();
            }
            return;
        }

        /* internal  */setSystemSetterInfo(arg1: string, arg2: Boolean=true, arg3: Boolean=false): void
        {
            if (arg2) 
            {
                if (SystemSetter.localSetter.isAlter && this._isReviceSet) 
                {
                    GameProxy.sysSetterProxy.saveSystemSetterInfo(arg1);
                    SystemSetter.localSetter.isAlter = false;
                }
            }
            SystemSetter.currentSetter.copy(SystemSetter.localSetter, arg3);
            this._sysSetInfoObj = SystemSetter.localSetter.getSetterObject();
            cache.autoResumeSet.roleLifeSet = SystemSetter.localSetter.roleLife;
            cache.autoResumeSet.roleManaSet = SystemSetter.localSetter.roleMana;
            cache.autoResumeSet.petLifeSet = SystemSetter.localSetter.petLife;
            cache.autoResumeSet.petManaSet = SystemSetter.localSetter.petMana;
            cache.autoResumeSet.isSaveSysset = true;
            if (this._isSet) 
            {
                MsgManager.showRollTipsMsg(Language.getString(33000));
                this._isSet = false;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.SysSettingSuccess));
            return;
        }

        /* internal  */onPlayerLockStatusHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SPlayerLockStatus;
            LockInfoData.lockStatus = loc1.status;
            if (!(this._systemModule == null) && !this._systemModule.isHide) 
            {
                this._systemModule.updateStatus();
            }
            return;
        }

        /* internal  */onRoleLevelUpdateHandler(arg1: Object): void
        {
            if (this._systemModule == null) 
            {
                return;
            }
            if (this._systemModule.isHaveLock) 
            {
                RolePlayer.instance.removeEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
                return;
            }
            if (cache.role.entityInfo.level >= 30 && !this._systemModule.isHide) 
            {
                this._systemModule.updateTabBar();
            }
            return;
        }

        /* internal  */onHeadChangeHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            if (loc1["id"] != "role") 
            {
                if (loc1["id"] == "pet") 
                {
                    this._sysSetterInfo.petLife = loc1["lifeSet"];
                    this._sysSetterInfo.petMana = loc1["manaSet"];
                }
            }
            else 
            {
                this._sysSetterInfo.roleLife = loc1["lifeSet"];
                this._sysSetterInfo.roleMana = loc1["manaSet"];
            }
            this.saveDataForSilderChange();
            return;
        }

        /* internal  */onPlayerSysShortcutHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SSysShortcut;
            ShortcutsKey.instance.setServerStr(loc1.jsStr);
            return;
        }

        /* internal  */onSaveShortcutSetter(arg1: DataEvent): void
        {
            var loc1=ShortcutsKey.instance.getServerStr();
            GameProxy.roleProxy.saveSysShortcut(loc1);
            MsgManager.showRollTipsMsg(Language.getString(33056));
            return;
        }

        private /* var */_sysSetInfoObj: Object;

        private /* var */_isSet: Boolean=false;

        private /* var */_isSetDefault: Boolean=false;

        private /* var */_isClientDefault: Boolean=false;

        private /* var */_sysSetterInfo: SystemSetterInfo;

        private /* var */_timer: Object /* flash.utils.Timer */;

        private /* var */_sysSetInfoStr: string;

        private /* var */_systemModule: ISystemSettingModule;

        private /* var */_isReviceSet: Boolean=false;

        private /* var */_isGuideInfoGot: Boolean=false;
    }
